﻿using MiniFox.Platform.Applications.Models;
using MiniFox.Platform.Models;
using Newtonsoft.Json;
using System.Security.Principal;

namespace MiniFox.Platform.Applications.Auth
{
    /// <summary>
    /// 
    /// </summary>
    public class SystemPrincipal : GenericPrincipal
    {
        string _admin;
        SystemIdentity _identity;
        string[] _roles;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="roles"></param>
        /// <param name="authorizeToken"></param>
        /// <param name="admin"></param>
        internal SystemPrincipal(SystemIdentity identity, string[] roles, AuthorizeToken authorizeToken, string admin) : base(identity, roles)
        {
            _identity = identity;
            _admin = admin;
            _roles = roles;
            this.AuthorizeToken = authorizeToken;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("globalId")]
        public string GlobalID
        {
            get { return _identity.GlobalID; }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("name")]
        public string Name
        {
            get { return _identity.Name; }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("displayName")]
        public string DisplayName
        {
            get { return _identity.DisplayName; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string[] UserRoles
        {
            get { return _roles; }
        }
        /// <summary>
        /// 
        /// </summary>
        public AuthorizeToken AuthorizeToken
        {
            get;
        }
        /// <summary>
        /// 
        /// </summary>
        public bool IsAdministrator
        {
            get
            {
                var isAdmin = this.Identity.Name == this._admin || this.IsInRole(this._admin);//账户名与管理员完整匹配 或 角色列表中包含管理员角色
                
                return isAdmin;
            }
        }
    }
}
